<?php require_once('Connections/RecursosHumanos.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO rol (ID_ROL, ID_DEPARTAMENTO, DES_ROL) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['ID_ROL'], "text"),
                       GetSQLValueString($_POST['ID_DEPARTAMENTO'], "text"),
                       GetSQLValueString($_POST['DES_ROL'], "text"));

  mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
  $Result1 = mysql_query($insertSQL, $RecursosHumanos) or die(mysql_error());

  $insertGoTo = "CrearRol.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO rol (ID_ROL, DES_ROL, ID_DEPARTAMENTO) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['ID_ROL'], "text"),
                       GetSQLValueString($_POST['DES_ROL'], "text"),
                       GetSQLValueString($_POST['ID_DEPARTAMENTO'], "text"));

  mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
  $Result1 = mysql_query($insertSQL, $RecursosHumanos) or die(mysql_error());
}

mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
$query_Deptos = "SELECT * FROM departamento ORDER BY departamento.ID_DEPARTAMENTO";
$Deptos = mysql_query($query_Deptos, $RecursosHumanos) or die(mysql_error());
$row_Deptos = mysql_fetch_assoc($Deptos);
$totalRows_Deptos = mysql_num_rows($Deptos);

$maxRows_Roles = 10;
$pageNum_Roles = 0;
if (isset($_GET['pageNum_Roles'])) {
  $pageNum_Roles = $_GET['pageNum_Roles'];
}
$startRow_Roles = $pageNum_Roles * $maxRows_Roles;

mysql_select_db($database_RecursosHumanos, $RecursosHumanos);
$query_Roles = "SELECT rol.ID_ROL, rol.DES_ROL, departamento.DES_DEPTO FROM rol, departamento WHERE rol.ID_DEPARTAMENTO = departamento.ID_DEPARTAMENTO ORDER BY rol.ID_ROL, rol.ID_DEPARTAMENTO";
$query_limit_Roles = sprintf("%s LIMIT %d, %d", $query_Roles, $startRow_Roles, $maxRows_Roles);
$Roles = mysql_query($query_limit_Roles, $RecursosHumanos) or die(mysql_error());
$row_Roles = mysql_fetch_assoc($Roles);

if (isset($_GET['totalRows_Roles'])) {
  $totalRows_Roles = $_GET['totalRows_Roles'];
} else {
  $all_Roles = mysql_query($query_Roles);
  $totalRows_Roles = mysql_num_rows($all_Roles);
}
$totalPages_Roles = ceil($totalRows_Roles/$maxRows_Roles)-1;

$queryString_Roles = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Roles") == false && 
        stristr($param, "totalRows_Roles") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Roles = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Roles = sprintf("&totalRows_Roles=%d%s", $totalRows_Roles, $queryString_Roles);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<style type="text/css">
<!--
body {
	margin-left: 2cm;
	margin-top: 1cm;
}
.Estilo1 {font-size: 12px}
-->
</style></head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <p><a href="home.html">REGRESAR</a></p>
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Id rol:</div></td>
      <td><input type="text" name="ID_ROL" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Descripci&ograve;n:</div></td>
      <td><input type="text" name="DES_ROL" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Departamento:</div></td>
      <td><select name="ID_DEPARTAMENTO">
          <option value=""></option>
          <?php
do {  
?>
          <option value="<?php echo $row_Deptos['ID_DEPARTAMENTO']?>"><?php echo $row_Deptos['DES_DEPTO']?></option>
          <?php
} while ($row_Deptos = mysql_fetch_assoc($Deptos));
  $rows = mysql_num_rows($Deptos);
  if($rows > 0) {
      mysql_data_seek($Deptos, 0);
	  $row_Deptos = mysql_fetch_assoc($Deptos);
  }
?>
      </select>      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td><input type="submit" value="Insertar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<table border="1" align="left">
  <tr>
    <td width="88">Id rol </td>
    <td width="150">Descripci&ograve;n</td>
    <td width="150">Departamento</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><span class="Estilo1"><?php echo $row_Roles['ID_ROL']; ?></span></td>
      <td><span class="Estilo1"><a href="ModificarRol.php?ID_ROL_MOD=<?php echo $row_Roles['ID_ROL']; ?>"><?php echo $row_Roles['DES_ROL']; ?></a></span></td>
      <td><span class="Estilo1"><?php echo $row_Roles['DES_DEPTO']; ?></span></td>
    </tr>
    <?php } while ($row_Roles = mysql_fetch_assoc($Roles)); ?>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
<table border="0" width="50%">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_Roles > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Roles=%d%s", $currentPage, 0, $queryString_Roles); ?>">Primero</a>
        <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_Roles > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Roles=%d%s", $currentPage, max(0, $pageNum_Roles - 1), $queryString_Roles); ?>">Anterior</a>
        <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Roles < $totalPages_Roles) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Roles=%d%s", $currentPage, min($totalPages_Roles, $pageNum_Roles + 1), $queryString_Roles); ?>">Siguiente</a>
        <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Roles < $totalPages_Roles) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Roles=%d%s", $currentPage, $totalPages_Roles, $queryString_Roles); ?>">&Uacute;ltimo</a>
        <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
</p>
</body>
</html>
<?php
mysql_free_result($Deptos);

mysql_free_result($Roles);
?>
